package com.itcam.servlet_;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Author:Cammy.Wu
 * Date:2024-06-27
 * Description: 响应行响应头响应体
 */

public class ServletDemo04Response extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("this is post");
        this.doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("this is get");
        // 设置响应头，按照key-value键值对的方式来设置，如果存在相同的key，会把value的值覆盖
        resp.setHeader("Server", "Tomcat");
        resp.setHeader("Server", "Apache");

        // 设置响应头，按照key-value键值对的方式来设置，如果存在相同的key，不会覆盖值
        resp.addHeader("Server", "Tomcat");
        resp.addHeader("Server", "Apache");

        // 服务端返回的对象数据要按照一定的格式要求进行渲染，只有是html格式才会识别标签
        resp.setHeader("Content-Type", "text/html;charset=utf-8");
        resp.setContentType("text/html");


        // 设置响应状态码
//        resp.sendError(404, "资源未找到");
        resp.getWriter().write("<h1>hello world</h1>");
    }
}
